package com.meteor.controller;

import com.meteor.framework.common.base.ResponseData;
import com.meteor.framework.common.utils.PageResult;
import com.meteor.framework.operatelog.annotations.OperateLog;
import com.meteor.framework.operatelog.enums.OperateTypeEnum;
import com.meteor.query.CommunityQuery;
import com.meteor.query.OrgAndCommunityQuery;
import com.meteor.query.UnitQuery;
import com.meteor.service.CommunityService;
import com.meteor.service.UnitService;
import com.meteor.vo.BuildingVO;
import com.meteor.vo.CommunityVO;
import com.meteor.vo.UnitVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author lynn
 * @date 2024/1/22
 */

@RestController
@RequestMapping("new/unit")
@Tag(name = "社区管理")
@AllArgsConstructor
public class UnitController {

    private final UnitService unitService;


    @GetMapping("page")
    @Operation(summary = "分页查询")
//    @PreAuthorize("hasAuthority('sys:publicity:page')")
    public ResponseData page(@ParameterObject @Valid UnitQuery query) {

        PageResult<UnitVO> page = unitService.page(query);

        return ResponseData.success(page);
    }

    @GetMapping("allList")
    @Operation(summary = "全部查询")
//    @PreAuthorize("hasAuthority('sys:publicity:all')")
    public ResponseData allList(UnitQuery query) {

        List<UnitVO> page = unitService.getList(query);

        return ResponseData.success(page);
    }


    @GetMapping("{id}")
    @Operation(summary = "信息")
//    @PreAuthorize("hasAuthority('sys:publicity:info')")
    public ResponseData get(@PathVariable("id") String id) {
        return ResponseData.success("查询成功", unitService.get(id));
    }

    @PostMapping
    @Operation(summary = "保存")
    @OperateLog(type = OperateTypeEnum.INSERT)
//    @PreAuthorize("hasAuthority('sys:publicity:save')")
    public ResponseData save(@RequestBody @Valid UnitVO vo) {
        unitService.save(vo);

        return ResponseData.success("新增成功");
    }

    @PutMapping
    @Operation(summary = "修改")
    @OperateLog(type = OperateTypeEnum.UPDATE)
//    @PreAuthorize("hasAuthority('sys:publicity:update')")
    public ResponseData update(@RequestBody @Valid UnitVO vo) {
        unitService.update(vo);

        return ResponseData.success("修改成功");
    }

    @DeleteMapping("{id}")
    @Operation(summary = "删除")
    @OperateLog(type = OperateTypeEnum.DELETE)
//    @PreAuthorize("hasAuthority('sys:publicity:delete')")
    public ResponseData delete(@PathVariable("id") String id) {
        unitService.delete(id);

        return ResponseData.success("删除成功");
    }


    @PostMapping("buildingUnit")
    @Operation(summary = "根据楼栋id查询单位")
//    @PreAuthorize("hasAuthority('sys:community:all')")
    public ResponseData buildingUnit(@RequestBody OrgAndCommunityQuery query) {
        List<UnitVO> list = unitService.buildingUnit(query);
        return ResponseData.success("查询成功",list);
    }

}
